package com.yuyou.fn.platform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mq.RedisMessageQueue;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.Constants;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.platform.mapper.SysUserMapper;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.JSONS;
import com.yuyou.fn.common.util.TimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.platform.mapper.AreaTeamMapper;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * AreaTeam 表数据服务层接口实现类
 *
 */
@Service
public class AreaTeamServiceImpl extends SuperServiceImpl<AreaTeamMapper, AreaTeam,Long> implements IAreaTeamService {

    @Override
    public void deleteAreaTeam(Long areaTeamId) {
        deleteById(areaTeamId);
    }

    @Override
    public Page<AreaTeam> findAreaTeamForPage(int pageNo, int pageSize) {
        return findPageByExample(AreaTeam.createExample(),pageNo,pageSize );
    }

    @Override
    public void saveOrUpdateAreaTeam(Long areaTeamId, String name , String province , String city ) {

        if( ( areaTeamId == null && name == null ) || (areaTeamId != null && name == null )  ){
            throw new IllegalArgumentException("Illegal arguments");
        }

        if(areaTeamId == null ){
            AreaTeam areaTeam = new AreaTeam();
            areaTeam.setName(name);
            areaTeam.setProvince(province);
            areaTeam.setCity(city);
            areaTeam.setCreateTime(TimeUtils.now());
            areaTeam.setUpdateTime(TimeUtils.now());
            checkAreaTeam(areaTeam);
            insertSelective(areaTeam);

        }else{
            AreaTeam areaTeam = new AreaTeam();
            areaTeam.setAreaTeamId(areaTeamId);
            areaTeam.setProvince(province);
            areaTeam.setCity(city);
            areaTeam.setName(name);
            areaTeam.setUpdateTime(TimeUtils.now());
            checkAreaTeam(areaTeam);
            updateSelectiveById(areaTeam);
        }

    }

    private void checkAreaTeam(AreaTeam areaTeam) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("name",areaTeam.getName());
        AreaTeam one = findOneByExample(example);
        if(one != null && (areaTeam.getAreaTeamId() == null || !areaTeam.getAreaTeamId().equals(one.getAreaTeamId()))){
            throw new BusException("区域名重复");
        }

    }

    @Override
    public List<AreaTeam> findAreaTeamForList(Long  areaTeamId) {

        Example<AreaTeam> example = AreaTeam.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,areaTeamId, null );
        return findListByExample(example);
    }

    @Override
    public String findTeamNameById(Long areaTeamId) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId);
        example.selectProperties("name");
        AreaTeam areaTeam = findOneByExample(example);
        return areaTeam == null ? StringUtils.EMPTY : areaTeam.getName() ;
    }

    @Override
    public AreaTeam findAreaTeamByName(String areaTeamName) {
        Example<AreaTeam> example = AreaTeam.createExample();
        example.createCriteria().andEqualTo("name",areaTeamName);
        return findOneByExample(example);
    }
}